<?php

    /**
     * Locale
     *
     * Selector for localisation string
     *
     * USAGE: "module~keyString"
     *
     * @package      Core
     * @subpackage   Selector
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
	class Core_Selector_Locale extends Core_Selector_Abstract
	{

    ////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * The language code
         *
         * @var string
         */
        protected $_locale = 'en_US';

        /**
         * The key of the translation
         *
         * @var string
         */
        protected $_messageKey = '';

        /**
         * The path to the cache file
         *
         * @var string
         */
        protected $_cachePath = '';

	////////////////////////////////////////////////////////////////////////////
	//                            Getters / Setters                           //
	////////////////////////////////////////////////////////////////////////////

		/**
         * Returns the language code
         *
         * @return      string
         */
        public function getLocale()
		{
			return $this->_locale;
		}

		/**
         * Returns the key of the translation
         *
         * @return      string
         */
        public function getMessageKey()
		{
			return $this->_messageKey;
		}

		/**
         * Returns the path to the cache file
         *
         * @return      string
         */
        public function getCachePath()
		{
			return $this->_cachePath;
		}

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

	    /**
		 * Constructor
		 *
		 * @param       string      $selector
		 * @param       string      $locale
		 * @throws		Core_Exception
		 * @throws		Core_ArgumentException
		 * @throws		Core_ArgumentNullException
	     */
	    public function __construct($selector, $locale = null)
		{
            if ($selector === null) {
				throw new Core_ArgumentNullException('selector is a null reference');
			}

            if (strlen(trim($selector)) == 0) {
				throw new Core_ArgumentException('selector is a zero-length string');
			}

            $this->_registry = Zend_Registry::getInstance();

			if ($locale === null) {
				$locale = $this->_registry['config']->view->locale;
			}

            if (strpos($locale, '_') === false) {
                $locale .= '_'. strtoupper($locale);
            }

            $this->_locale = $locale;

            if (preg_match("/^(([a-zA-Z0-9_\.]+)~)([a-zA-Z0-9_\.]+)$/", $selector, $m)) {
                if (!empty($m[1]) && !empty($m[2])) {
                    $this->_module = ucfirst(strtolower($m[2]));
                } else {
                    $this->_module = Core_Context::get();
                }

                $this->_messageKey = $m[3];

                $this->_createPath();
                $this->_createCachePath();
            } else {
                throw new Core_Exception('Invalid locale selector syntax ('. $selector .')');
            }
	    }

	////////////////////////////////////////////////////////////////////////////
	//                            Protected methods                           //
	////////////////////////////////////////////////////////////////////////////

	    /**
         * Sets the path to the file
         *
         * @return      void
         */
        protected function _createPath()
		{
			$path = Core_IO_Path::combine(
                                            PROJECT_MODULE_PATH,
                                            ucfirst(strtolower($this->_module)),
                                            'locales',
                                            $this->_registry['end'],
                                            $this->_locale .'.csv'
                                        );
	    
            if (!Core_IO_File::exists($path)) {
                $path = Core_IO_Path::combine(
                                                SERUM_MODULE_PATH,
                                                ucfirst(strtolower($this->_module)),
                                                'locales',
                                                $this->_registry['end'],
                                                $this->_locale .'.csv'
                                            );
            }

            $this->_path = $path;
        }

	    /**
         * Sets the path to the cache file
         *
         * @return      void
         */
        protected function _createCachePath()
		{
			$this->_cachePath = Core_IO_Path::combine(
                                                      PROJECT_TEMP_LOCALES,
                                                      '~'
                                                      . strtolower($this->_module)
                                                      . '~'
                                                      . $this->_registry['end']
                                                      . '~'
                                                      . $this->_locale
                                                      . '~'
                                );
	    }
	}
